import { DrugClient, InOutputUsageDrugModel, InOutputUsageDrugModelInfoModel, InputGetListForDrugModel, InputInsertForDrugModel, InputSearchForDrugModel, OutPutDrugInfo, OutputSearchForDrug, OutputSearchForDrugItem } from "@/api/api_drugclient";
import { ApiExceptionShow } from "@/framework/utils/api-exception-show";
import notify from "devextreme/ui/notify";
import { Vue } from "vue-class-component";
/*
 * @Author: LAB
 * @Date: 2021-05-26 17:02:18
 * @Description: 药品资源
 * @FilePath: \client-app\src\data-source\drug.ts
 */
export interface ListDataI<T> {
  data: T[],
  dataCount: number;
}
export default class DrugData {
  client: DrugClient;
  view: Vue
  constructor(client: DrugClient, view: Vue) {
    this.client = client;
    this.view = view;
  }
  getList(body: InputGetListForDrugModel): Promise<ListDataI<OutPutDrugInfo>> {
    return this.client.getList(body).then(res => {
      if (res.isSuccess) {
        return {
          data: res.listData,
          dataCount: res.pageInfo.count
        }
      } else {
        notify(res.message, "error")
        return {
          data: [],
          dataCount: 0
        }
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {
        data: [],
        dataCount: 0
      }
    })
  }
  // 搜索定位
  search(body: InputSearchForDrugModel, type: "index" | "drug" = "index"): Promise<ListDataI<OutputSearchForDrug | OutPutDrugInfo>> {
    return this.client.positioningForDrug(body).then(res => {
      if (res.isSuccess) {
        let listData = res.listData.map(item => {
          if (type == "drug") {
            return item.drugInfo
          } else {
            return item
          }
        })
        return {
          data: listData,
          dataCount: res.pageInfo.count
        }
      } else {
        notify(res.message, "error")
        return {
          data: [],
          dataCount: 0
        }
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {
        data: [],
        dataCount: 0
      }
    })
  }
  // 搜索
  searchByNameWBPY(searchTest: string): Promise<ListDataI<OutputSearchForDrugItem>> {
    return this.client.searchForDrug(searchTest).then(res => {
      if (res.isSuccess) {
        return {
          data: res.listData,
          dataCount: res.listData.length
        }
      } else {
        notify(res.message, "error")
        return {
          data: [],
          dataCount: 0
        }
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {
        data: [],
        dataCount: 0
      }
    })
  }

  submit(body: InputInsertForDrugModel) {
    return this.client.submit(body)
  }
  delete(id: string) {
    return this.client.delete(id).then(res => {
      if (res.isSuccess) {
        notify("删除成功", "success")
      } else {
        notify(res.message, "error")
      }
      return res
    }).catch(err => {
      ApiExceptionShow.showException(err, this.view)
    })
  }
  info(drugId: string, schemeId: string): Promise<OutPutDrugInfo> {
    return this.client.info(drugId, schemeId).then(res => {
      if (res.isSuccess) {
        return res.data
      } else {
        notify(res.message, "error")
        return {}
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {}
    })
  }
  
  // 添加用法详情
  addDosage(body: any) {
    return this.client.submitForUsageDrug(body).then(res => {
      if (res.isSuccess) {
        notify(res.message, "success")
      } else {
        notify(res.message, "error")
      }
      return res
    }).catch(err => {
      ApiExceptionShow.showException(err, this.view)
    })
  }

  // 获取用法详情
  getDosage(id: string): Promise<InOutputUsageDrugModel> {
    return this.client.getUsageDrugInfo(id).then(res => {
      if (res.isSuccess) {
        return res.data
      } else {
        notify(res.message, "error")
        return {} as InOutputUsageDrugModel
      }
    }).catch(err => {
      ApiExceptionShow.showException(err, this.view)
      return {} as InOutputUsageDrugModel
    })
  }
}